package cn.tedu.logistics.service;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.tedu.logistics.bean.Depository;
import cn.tedu.logistics.bean.InputInfo;
import cn.tedu.logistics.bean.OrderInfo;
import cn.tedu.logistics.dao.DepositoryDao;
import cn.tedu.logistics.dao.InputInfoDao;
import cn.tedu.logistics.dao.OrderInfoDao;

@Service
public class OrderServicempI implements OrderService {
	@Autowired
	private OrderInfoDao orderInfoDao;
	@Autowired
	private InputInfoDao inputDao;
	@Autowired
	private DepositoryDao depoDao;
	public List<OrderInfo> findAll() {// 查询全部
		List<OrderInfo> list = orderInfoDao.selectList(null);
		return list;
	}

	@Override
	public List<OrderInfo> getOrder(OrderInfo order) {
		QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<OrderInfo>(order);
		List<OrderInfo> list = null;
		list = orderInfoDao.selectList(queryWrapper);
		System.out.println(list);
		return list;
	}

	@Override
	public void addorder(OrderInfo orderInfo) {
		orderInfoDao.insert(orderInfo);
	}

	@Override
	public OrderInfo getorderById(String orderid) {
		return orderInfoDao.selectById(orderid);
	}
	
	@Override
	public void updateOrder(OrderInfo orderInfo) {
		orderInfoDao.updateById(orderInfo);
	}
	@Override
	public void confirm(String orderId) {
		OrderInfo order =orderInfoDao.selectById(orderId);
		order.setOrderStatus("已确认");
		orderInfoDao.updateById(order);
	}
	//确认订单实现
	@Override
	@Transactional
	public void execute(String orderId) {
		OrderInfo order =orderInfoDao.selectById(orderId);
		if(order!=null) {
			order.setOrderStatus("已实现");
			orderInfoDao.updateById(order);
			//确认订单后，自动新增送货单
			InputInfo inputInfo = new InputInfo();
			String result;
			Random random = new Random();
			do {
				result="";
				for (int i=0;i<10;i++)
				{
				    result+=random.nextInt(10);
				}
			}while(inputDao.selectById(result)!=null);
			//生成一个InputInfo对象
			inputInfo.setInputid(result);
			inputInfo.setGoodsname(order.getGoodsname());
			Integer num = order.getGoodsnum();
			inputInfo.setGoodsnum(num);
			String shopId = order.getShopid();
			inputInfo.setShopid(shopId);
			inputInfo.setCreatetime(new Date());
			inputInfo.setUpdatetime(inputInfo.getCreatetime());
			inputDao.insert(inputInfo);
			//自动更新库存信息
			String goodsName = order.getGoodsname();
			QueryWrapper<Depository> queryWrapper = new QueryWrapper<Depository>();
			queryWrapper.eq("shop_id", shopId);
			queryWrapper.eq("goods_name", goodsName);
			Depository depo = depoDao.selectOne(queryWrapper);
			if(depo!=null) {
				//如果已经存在库存信息，则增加数量
				depo.setGoodsInventory(depo.getGoodsInventory()+num);
				depoDao.updateById(depo);
			}else {
				//如果不存在库存信息，新增一条库存信息
				Depository depo1 = new Depository();
				depo1.setGoodsInventory(num);
				depo1.setShopId(shopId);
				depo1.setGoodsName(goodsName);
				depo1.setUpdateTime(new Date());
				depoDao.insert(depo1);
			}
		}

	}
}
